/**
  ******************************************************************************
  * Copyright © Beijing WIIP Co., Ltd. 2022-2024. All rights reserved.
  ******************************************************************************
  * @file: diskio.c
  * @brief: FATFS文件系统和硬件驱动层接口文件
  *
  ******************************************************************************
  * @note:
  *
  ******************************************************************************
  * Change Logs:
  * Date           Author       Notes
  * 2024-07-26     MBL       the first version
  *
  *
  ******************************************************************************
  */

/* 声明包含头文件 --------------------------------------------------------------*/
#include "rtc2.h"
#include "usbh_diskio.h"
#include "../../FTL/ftl.h"
#include "diskio.h"
#include "rtc.h"
#include "nand.h"
/* 声明私有类型定义--------------------------------------------------------------*/

/* 声明私有变量 ---------------------------------------------------------------*/

uint32_t NAND_FLASH_SECTOR_COUNT;
uint8_t NAND_FLASH_BLOCK_SIZE;
/* 声明私有常量 ---------------------------------------------------------------*/

/* 声明私有宏定义 --------------------------------------------------------------*/
#define EX_NAND     0         /* 外部NAND FLASH卷标为0 */
#define USB_DISK    1         /* U盘，卷标为1 */
//#define SD_CARD     2         /* SD卡,卷标为2 */
//#define EX_FLASH    3         /* 外部spi flash,卷标为3 */

#define FLASH_BLOCK_SIZE  8 	/* 每个BLOCK有8个扇区 */

//#define SPI_FLASH_SECTOR_SIZE   512
//#define SPI_FLASH_SECTOR_COUNT  25 * 1024 * 2
//#define SPI_FLASH_BLOCK_SIZE    8
//#define SPI_FLASH_FATFS_BASE    0
/* 声明私有函数 ---------------------------------------------------------------*/


/**
 * @brief       获得磁盘状态
 * @param       pdrv : 磁盘编号0~9
 * @retval      None
 */
DSTATUS disk_status (
    BYTE pdrv       /* Physical drive number to identify the drive */
)
{
    return RES_OK;
}

/**
 * @brief       初始化磁盘
 * @param       pdrv : 磁盘编号0~9
 * @retval      None
 */
DSTATUS disk_initialize (
    BYTE pdrv       /* Physical drive number to identify the drive */
)
{
    uint8_t res = 0;

    switch (pdrv)
    {
        case EX_NAND:                /* 外部nand */
        	res = nand_init();
//        	res = ftl_init();		         /* 初始化FTL分区 */
            break;

        case USB_DISK:               /* U盘 */
        	res = USBH_initialize();
            break;

//        case SD_CARD:                /* SD卡 */
////          res = sd_init();
//            break;
//
//        case EX_FLASH:               /* 外部flash */
////          norflash_init();
//            break;

        default:
            res = 1;
            break;
    }

    if (res)
    {
        return  STA_NOINIT;
    }
    else
    {
        return 0; /* 初始化成功*/
    }
}

/**
 * @brief       读扇区
 * @param       pdrv   : 磁盘编号0~9
 * @param       buff   : 数据接收缓冲首地址
 * @param       sector : 扇区地址
 * @param       count  : 需要读取的扇区数
 * @retval      None
 */
DRESULT disk_read (
    BYTE pdrv,      /* Physical drive number to identify the drive */
    BYTE *buff,     /* Data buffer to store read data */
    DWORD sector,   /* Sector address in LBA */
    UINT count      /* Number of sectors to read */
)
{
    uint8_t res = 0;

    if (!count)return RES_PARERR;   /* count���ܵ���0�����򷵻ز������� */

    switch (pdrv)
    {
        case EX_NAND:       /* 外部nand */
        	res = ftl_read_sectors(buff, sector, 512, count);
//        	res = ftl_read_sectors(buff, sector, 2048, count);
            break;

        case USB_DISK:      /* U盘 */
        	res = USBH_read(buff, sector, count);
            break;

//        case SD_CARD:      /* SD卡 */
////            res = sd_read_disk(buff, sector, count);
////
////            while (res)     /* 读出错 */
////            {
////                sd_init();  /* 重新初始化SD卡 */
////                res = sd_read_disk(buff, sector, count);
////            }
//            break;
//
//        case EX_FLASH:     /* 外部flash */
////            for (; count > 0; count--)
////            {
////                norflash_read(buff, SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE, SPI_FLASH_SECTOR_SIZE);
////                sector++;
////                buff += SPI_FLASH_SECTOR_SIZE;
////            }
////            res = 0;
//            break;

        default:
            res = 1;
    }

    /* 处理返回值，将返回值转成ff.c的返回值 */
    if (res == 0x00)
    {
        return RES_OK;
    }
    else
    {
        return RES_ERROR; 
    }
}

/**
 * @brief       写扇区
 * @param       pdrv   : 磁盘编号0~9
 * @param       buff   : 发送数据缓存区首地址
 * @param       sector : 扇区地址
 * @param       count  : 需要写入的扇区数
 * @retval      None
 */
DRESULT disk_write (
    BYTE pdrv,          /* Physical drive number to identify the drive */
    const BYTE *buff,   /* Data to be written */
    DWORD sector,       /* Sector address in LBA */
    UINT count          /* Number of sectors to write */
)
{
    uint8_t res = 0;

    if (!count)return RES_PARERR;   /* count���ܵ���0�����򷵻ز������� */

    switch (pdrv)
    {
    	case EX_NAND:                /* 外部nand */
        	res = ftl_write_sectors((uint8_t *)buff, sector, 512, count);
    		break;

    	case USB_DISK:               /* U盘 */
        	res = USBH_write(buff, sector, count);
    		break;

//    	case SD_CARD:                /* SD卡 */
////            res = sd_write_disk((uint8_t *)buff, sector, count);
////
////            while (res)     /* 写出错 */
////            {
////                sd_init();  /* 重新初始化SD卡 */
////                res = sd_write_disk((uint8_t *)buff, sector, count);
////            }
//    		break;
//
//    	case EX_FLASH:               /* 外部flash */
////            for (; count > 0; count--)
////            {
////                norflash_write((uint8_t *)buff, SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE, SPI_FLASH_SECTOR_SIZE);
////                sector++;
////                buff += SPI_FLASH_SECTOR_SIZE;
////            }
////            res = 0;
//    		break;

        default:
            res = 1;
    }

    /* 处理返回值，将返回值转成ff.c的返回值 */
    if (res == 0x00)
    {
        return RES_OK;
    }
    else
    {
        return RES_ERROR; 
    }
}

/**
 * @brief       获取其他控制参数
 * @param       pdrv   : 磁盘编号0~9
 * @param       ctrl   : 控制代码
 * @param       buff   : 发送/接收缓冲区指针
 * @retval      None
 */
DRESULT disk_ioctl (
    BYTE pdrv,      /* Physical drive number (0..) */
    BYTE cmd,       /* Control code */
    void *buff      /* Buffer to send/receive control data */
)
{
    DRESULT res;
    if (pdrv == EX_NAND)    /* 外部NAND */
    {
        switch (cmd)
        {
            case CTRL_SYNC:
                res = RES_OK;
                break;

            case GET_SECTOR_SIZE:
                *(WORD *)buff = 512;      						 /* NAND FLASH扇区强制为512字节大小 */
                res = RES_OK;
                break;

            case GET_BLOCK_SIZE:
                *(WORD *)buff = nand_dev.page_mainsize / 512;    /* block大小，定义成一个page的大小 */
                res = RES_OK;
                break;

            case GET_SECTOR_COUNT:
                *(DWORD *)buff = nand_dev.valid_blocknum * nand_dev.block_pagenum * nand_dev.page_mainsize / 512;    /* NAND FLASH的总扇区大小 */
                res = RES_OK;
                break;

            default:
                res = RES_PARERR;
                break;
        }
    }
    else if (pdrv == USB_DISK)  /* U盘 */
    {
    	res = USBH_ioctl(cmd, buff);
    }
//    else if (pdrv == SD_CARD)  /* SD卡 */
//    {
//        switch (cmd)
//        {
//            case CTRL_SYNC:
//                res = RES_OK;
//                break;
//
//            case GET_SECTOR_SIZE:
//                *(DWORD *)buff = 512;
//                res = RES_OK;
//                break;
//
//            case GET_BLOCK_SIZE:
//                *(WORD *)buff = g_sd_card_info_handle.LogBlockSize;
//                res = RES_OK;
//                break;
//
//            case GET_SECTOR_COUNT:
//                *(DWORD *)buff = g_sd_card_info_handle.LogBlockNbr;
//                res = RES_OK;
//                break;
//
//            default:
//                res = RES_PARERR;
//                break;
//        }
//    }
//    else if (pdrv == SD_CARD)  /* 外部flash */
//    {
//        switch (cmd)
//        {
//            case CTRL_SYNC:
//                res = RES_OK;
//                break;
//
//            case GET_SECTOR_SIZE:
//                *(WORD *)buff = SPI_FLASH_SECTOR_SIZE;
//                res = RES_OK;
//                break;
//
//            case GET_BLOCK_SIZE:
//                *(WORD *)buff = SPI_FLASH_BLOCK_SIZE;
//                res = RES_OK;
//                break;
//
//            case GET_SECTOR_COUNT:
//                *(DWORD *)buff = SPI_FLASH_SECTOR_COUNT;
//                res = RES_OK;
//                break;
//
//            default:
//                res = RES_PARERR;
//                break;
//        }
//    }
    else
    {
        res = RES_ERROR;    /* �����Ĳ�֧�� */
    }
    
    return res;
}
/**
 * @brief       获取系统运行时间函数
 * @param       None
 * @retval      实时时间
 */
DWORD get_fattime (void)
{
	/* USER CODE BEGIN get_fattime */
	 uint32_t retValue = 0;
	 uint8_t year = 0,month = 0,week=0, day = 0,hour = 0,minuite = 0,second = 0,ampm = 0;
	 rtc_get_date(&year, &month, &day, &week);
     rtc_get_time(&hour, &minuite, &second, &ampm);
     year = 2000 + year - 1980;
	 retValue = (uint32_t)year<<25 | (uint32_t)month<<21 | (uint32_t)day<<16 | (uint32_t)hour<<11 | (uint32_t)minuite<<5 | second;
	 return retValue;
	 /* USER CODE END get_fattime */
}

